from django.db import models
from django.utils import timezone

# Create your models here.


class PubName(models.Model):
    pubname = models.CharField("名称", max_length=50, unique=True)


class Book(models.Model):
    title = models.CharField(max_length=30, verbose_name='书名')
    # public = models.CharField(max_length=50, verbose_name='出版社')
    describe = models.TextField(verbose_name="描述", default='')
    price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='定价')
    retail_price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='零售价', default=30.0)
    pub = models.ForeignKey(to=PubName, on_delete=models.CASCADE, null=True)
    authors = models.ManyToManyField(to="Author")
    cover_uri = models.CharField(max_length=255, verbose_name="封面", default='')
    pub_date = models.DateField(null=True, verbose_name="出版日期")
    create_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return "title:%s pub:%s price:%s" % (self.title, self.pub, self.price)


class Author(models.Model):
    name = models.CharField(max_length=30, db_index=True)
    company = models.CharField(max_length=240,  null=True)
    email = models.EmailField(null=True)
    # books = models.ManyToManyField(to="Book")
    create_time = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return '作者: %s' % self.name


class UserInfo(models.Model):
    role = models.CharField(max_length=2, default='1')
    username = models.CharField(max_length=24)
    password = models.CharField(max_length=33)
    choices = (
        ('male', "男性"),
        ('female', "女性")
    )
    gender = models.CharField(max_length=10, choices=choices, default='male')
    create_time = models.DateTimeField(default=timezone.now)


class ExtendUserInfo(models.Model):
    user = models.OneToOneField(to=UserInfo, on_delete=models.CASCADE)
    signature = models.CharField(max_length=225)
    nickname = models.CharField(max_length=225)
    avatar = models.CharField(max_length=255, null=True)
    create_time = models.DateTimeField(default=timezone.now)


class BookComment(models.Model):
    user = models.ForeignKey(to=UserInfo, on_delete=models.CASCADE)
    book = models.ForeignKey(to=Book, on_delete=models.CASCADE)
    score = models.IntegerField()
    content = models.TextField()
    create_time = models.DateTimeField(default=timezone.now)


class CommentLike(models.Model):
    class Meta:
        unique_together = ["comment", "user"]

    user = models.ForeignKey(to=UserInfo, on_delete=models.CASCADE)
    book = models.ForeignKey(to=Book, on_delete=models.CASCADE)
    comment = models.ForeignKey(to=BookComment, on_delete=models.CASCADE)
    create_time = models.DateTimeField(default=timezone.now)


class BookCollect(models.Model):
    class Meta:
        unique_together = ["book", "user"]

    user = models.ForeignKey(to=UserInfo, on_delete=models.CASCADE)
    book = models.ForeignKey(to=Book, on_delete=models.CASCADE)
    create_time = models.DateTimeField(default=timezone.now)
